import { server } from '../config/server';
import { useToken } from '../utils/token';

export const task = {
    // 获取所有任务计划列表
    getTaskList() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/task/getTaskList`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request getTaskList 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getTaskList 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    // 添加任务计划
    addTask(taskName, taskDesc, taskCount) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/task/addTask`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    name: taskName,
                    description: taskDesc,
                    count: taskCount
                },
                success: (res) => {
                    console.log('【 Response in request addTask 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request addTask 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    // 计划打卡
    setTaskComplete(taskId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/task/setTaskComplete`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    taskId
                },
                success: (res) => {
                    console.log('【 Response in request setTaskComplete 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request setTaskComplete 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    deleteTask(taskId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/task/deleteTask`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: {
                    taskId
                },
                success: (res) => {
                    console.log('【 Response in request deleteTask 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request deleteTask 】', err);
                    reject({ success: false, err });
                }
            });
        });
    }
};